import { ref, unref } from "vue";
import type { Ref } from "vue";
import {
  cancelApi,
  activityDetailApi,
  userDrawInfoApi,
  userDrawResidueCountApi,
  checkDrawEligibleApi,
  createActivityOrderApi,
} from "@/api/lucky";
type Prize = any; // 根据实际情况定义奖品类型
type DrawCallback = (prizeIndex: number) => void;

export interface UseLotteryOptions {
  onDrawStart?: () => void;
  onDrawEnd?: () => void;
}

export function useLottery(
  activityId: Ref<string>,
  code: Ref<string>,
  options: UseLotteryOptions
) {
  const { onDrawStart, onDrawEnd } = options;
  const isDrawing = ref(false);
  const prizeIndex = ref<number | null>(null);
  const prize = ref<Prize | null>(null);
  const drawResidueCount = ref(0);
  const activityPrizeList = ref<Prize[]>([]);
  const drawTemplate = ref<any>([]);
  const activityDetail = ref<any>({});
  const prizeTempId = ref<string | null>(null);
  const isShowAwd = ref(false);
  const startDraw = async () => {
    if (isDrawing.value) return;
    await checkDrawEligibleApi({
      activityId: unref(activityId),
      code: unref(code),
    });
    getUserDrawInfo();
    if (drawResidueCount.value <= 0) return;
    isDrawing.value = true;
    onDrawStart?.();
  };

  const endDraw = async () => {
    isDrawing.value = false;
    isShowAwd.value = true;
    await cancelApi({
      code: unref(code),
      activityId: unref(activityId),
      prizeTempId: unref(prizeTempId),
    });

    onDrawEnd?.();
  };

  const getUserDrawInfo = async () => {
    const data = await userDrawInfoApi({
      activityId: unref(activityId),
      code: unref(code),
    });
    prizeIndex.value = data.prizeIndex;
    prize.value = data.prize;
    prizeTempId.value = data.prizeTempId;
    drawResidueCount.value = await userDrawResidueCountApi({
      activityId: unref(activityId),
      code: unref(code),
    }).then((res) => res.userDrawResidueCount);
  };
  function loadActivity() {
    activityDetailApi({ activityId: activityId.value }).then((res: any) => {
      activityDetail.value = res.activity;
      activityPrizeList.value = res.activityPrizeList;
      drawTemplate.value = res.drawTemplate;
    });
  }
  const getUserDrawResidueCount = async () => {
    drawResidueCount.value = await userDrawResidueCountApi({
      activityId: unref(activityId),
      code: unref(code),
    }).then((res) => res.userDrawResidueCount);
  };
  const toClaimPages = async () => {
    const data = await await createActivityOrderApi({
      code: code.value,
      activityId: activityId.value,
      prizeTempId: prizeTempId.value,
      prizeId: prize.value.id,
    });
    uni.navigateTo({
      url: `/pages/orderDetail/index?orderId=${data.order.id}`,
    });
    isShowAwd.value = false;
  };
  const init = () => {
    loadActivity();
    getUserDrawResidueCount();
  };
  return {
    isDrawing,
    prizeIndex,
    prize,
    activityPrizeList,
    activityDetail,
    drawResidueCount,
    isShowAwd,
    toClaimPages,
    startDraw,
    endDraw,
    init,
  };
}
